package com.kingsalert.kingsalert.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.kingsalert.kingsalert.R;

public class RingProgressBar extends View {

    private Paint backgroundPaint, progressPaint;
    private int backgroundColor, progressColor;

    private float ringWidth;
    private int max;
    private int progress;

    public RingProgressBar(Context context) {
        this(context,null);
    }

    public RingProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
        init(context,attrs);
    }

    public RingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){

        if (attrs != null) {
            @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RingProgressBar);
            backgroundColor = typedArray.getColor(R.styleable.RingProgressBar_ringColor, Color.GRAY);
            progressColor = typedArray.getColor(R.styleable.RingProgressBar_ringProgressColor, Color.GREEN);
            ringWidth = typedArray.getDimension(R.styleable.RingProgressBar_ringWidth,20);
            max = typedArray.getInteger(R.styleable.RingProgressBar_max,100);
            //资源回收
            typedArray.recycle();
        }

        backgroundPaint = new Paint();
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeWidth(ringWidth);
        backgroundPaint.setAntiAlias(true);

        progressPaint = new Paint();
        progressPaint.setColor(progressColor);
        progressPaint.setStrokeWidth(ringWidth);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setAntiAlias(true);
        progressPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int centerX = getWidth()/2;
        int centerY = getHeight()/2;
        int radius = (int)(centerX - ringWidth/2);
        //绘制背景圆
        canvas.drawCircle(centerX,centerY,radius, backgroundPaint);
        //绘制进度圆
        @SuppressLint("DrawAllocation") RectF rectF = new RectF(centerX-radius,centerY-radius,centerX+radius,centerY+radius);
        canvas.drawArc(rectF, -90, (float) (progress*360/max), false, progressPaint);
    }

    public synchronized  int getMax(){
        return max;
    }

    public synchronized int getProgress(){
        return progress;
    }

    public synchronized void setProgress(int progress){

        Log.w("---testje---", "setProgress: "+progress );

        if (progress<0) return;
        if (progress>max) progress = max;
        this.progress = progress;
        postInvalidate();
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public void setRingWidth(float ringWidth) {
        this.ringWidth = ringWidth;
    }

}
